package net.akcegaraz.tipleague.client.presenter;

import java.util.HashMap;

import net.akcegaraz.tipleague.client.service.TipServiceAsync;
import net.akcegaraz.tipleague.shared.domain.Ticket;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class RoundPresenter implements Presenter {

    public interface Display {
	Widget asWidget();

	void viewRound(HashMap<String, Ticket> tickets);
    }

    private final TipServiceAsync rpcService;
    private final HandlerManager eventBus;
    private final Display display;

    public RoundPresenter(TipServiceAsync rpcService, HandlerManager eventBus,
	    Display display) {
	this.rpcService = rpcService;
	this.eventBus = eventBus;
	this.display = display;
    }

    public void go(HasWidgets container) {
	container.clear();
	container.add(display.asWidget());
	fetchListTickets();
    }

    private void fetchListTickets() {
	rpcService.listTickets(new AsyncCallback<HashMap<String, Ticket>>() {

	    public void onFailure(Throwable arg0) {
		Window.alert("Error fetch list tickets");
	    }

	    public void onSuccess(HashMap<String, Ticket> result) {
		display.viewRound(result);
	    }
	});

    }

}
